package Quitz::Core::Test;

use base qw(Test::Class);
use lib qw(lib);
use Quitz::Core;
use Test::More;
use Data::Dumper;

sub db_connect : Test(startup => 1) {
    my $this = shift;
    $this->{core} = new Quitz::Core
    (
        -ConnectInfo =>
        {
            'dsn' => 'dbi:SQLite:dbname=test.db',
            username => '',
            password => '',
            storage_params =>
            {
                RaiseError => 1
            }
        }
    );
    my $users_rs = $this->{core}->resultset('User');
    while (my $user = $users_rs->next) {}
    ok($this->{core}->storage->connected, "database connection");
};

sub db_disconnect : Test(shutdown) {
    my $this = shift;

    $this->{core}->storage->disconnect;
};

sub check_user_functionality : Test(4) {
    my $this = shift;
    my $user_data = 
    { 
        generic_rights => 2,
        username    => 'test',  
        password => 'testpass' 
    };
    my $test = $this->{core}->resultset('User')->create( $user_data );

    my $id = $test->get_column('id'); 
    ok(defined($id), "checking user insert");

    my $user = $this->{core}->resultset('User')->single( $user_data );
    is($user->get_column('password'), 'testpass', "checking user select");

    my $success = $user->update({username => 'test_updated'});
    ok($success, "checking user update");
    
    $user->delete();
    ok($user->in_storage() == 0, "checking user delete");
};

sub check_category_functionality : Test(4) {
    my $this = shift;
    my $category_data = 
    { 
        parent_id => 0,
        owner    => 'testowner',  
        name => 'test' 
    };
    my $test = $this->{core}->resultset('Category')->create( $category_data );

    my $id = $test->get_column('id'); 
    ok(defined($id), "checking category insert");

    my $category = $this->{core}->resultset('Category')->single( $category_data );
    is($category->get_column('name'), 'test', "checking category select");

    my $success = $category->update({name => 'test_updated'});
    ok($success, "checking category update");
    
    $category->delete();
    ok($category->in_storage() == 0, "checking category delete");
};

sub check_question_functionality : Test(4) {
    my $this = shift;
    my $question_data = 
    { 
        category_id => 0,
        content_id => 0,
        weight => 1,
        difficulty => 2,
        is_mutable => false
    };
    my $test = $this->{core}->resultset('Question')->create( $question_data );

    my $id = $test->get_column('id'); 
    ok(defined($id), "checking question insert");

    my $question = $this->{core}->resultset('Question')->single( $question_data );
    is($question->get_column('difficulty'), 2, "checking question select");

    my $success = $question->update({difficulty => 3});
    ok($success, "checking question update");
    
    $question->delete();
    ok($question->in_storage() == 0, "checking question delete");
};

sub check_answer_functionality : Test(4) {
    my $this = shift;
    my $answer_data = 
    { 
        question_id => 0,
        content_id => 0,
        is_correct => true,
        show_order => 0
    };
    my $test = $this->{core}->resultset('Answer')->create( $answer_data );

    my $id = $test->get_column('id'); 
    ok(defined($id), "checking answer insert");

    my $answer = $this->{core}->resultset('Answer')->single( $answer_data );
    is($answer->get_column('is_correct'), true, "checking answer select");

    my $success = $answer->update({is_correct => false});
    ok($success, "checking answer update");
    
    $answer->delete();
    ok($answer->in_storage() == 0, "checking answer delete");
};

sub check_content_functionality : Test(4) {
    my $this = shift;
    my $content_data = 
    { 
        type => 'text',
        content => 'test content'
    };
    my $test = $this->{core}->resultset('Content')->create( $content_data );

    my $id = $test->get_column('id'); 
    ok(defined($id), "checking content insert");

    my $content = $this->{core}->resultset('Content')->single( $content_data );
    is($content->get_column('content'), 'test content', "checking content select");

    my $success = $content->update({content => 'updated content'});
    ok($success, "checking content update");
    
    $content->delete();
    ok($content->in_storage() == 0, "checking content delete");
};

sub check_acl_functionality : Test(3) {
    my $this = shift;
    my $acl_data = 
    { 
        user_id => 1,
        category_id => 0,
        access_mask => 3
    };
    my $test = $this->{core}->resultset('ACL')->create( $acl_data );

#    my $id = $test->get_column('user_id'); 
#    ok(defined($id), "checking acl insert");

    my $acl = $this->{core}->resultset('ACL')->single( $acl_data );
    is($acl->get_column('access_mask'), 3, "checking acl select");

    my $success = $acl->update({access_mask => 1});
    ok($success, "checking acl update");
    
    $acl->delete();
    ok($acl->in_storage() == 0, "checking acl delete");
};

1;